//app.js

import { sendUserInfo } from './pages/script/server'

App({
  onLaunch: function (options) {
    this.fnLogin()
    .then(login => {
      console.log(login);
      this.getUserInfo(login, options);
    }, fail => {
      console.log('fail');
      this.showSetting(this.getUserInfo(fail, options));
    }) 


    // // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    
    // wx.login({
    //   success: res => {
    //     console.log(res);
    //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
    //   }
    // })
    // // 获取用户信息
    // wx.getSetting({
    //   success: res => {
    //     if (res.authSetting['scope.userInfo']) {
    //       console.log('scope.userInfo');
    //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
    //       wx.getUserInfo({
    //         success: res => {
    //           // 可以将 res 发送给后台解码出 unionId
    //           this.globalData.userInfo = res.userInfo
    //           console.log(res.userInfo);
    //           // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //           // 所以此处加入 callback 以防止这种情况
    //           if (this.userInfoReadyCallback) {
    //             this.userInfoReadyCallback(res)
    //           }
    //         }
    //       })
    //     }
    //   }
    // })
  },

 // 登录
  fnLogin(){
    console.log('fnLogin');
    return new Promise((resolve, reject) => {
      wx.login({
        success: res => {
          console.log('loginRes',res);
          let loginRes = res;
          wx.setStorageSync('login_code', res.code);
          wx.getSetting({
            success: setRes => {
              console.log('setRes',setRes);
              // 判断是否已授权
              if (setRes.authSetting['scope.userInfo']) {
                console.log('scope.userInfo');
                wx.setStorageSync('scopeUserInfo',true); // 已授权
                resolve(loginRes)
              }else{
                wx.setStorageSync('scopeUserInfo',false); // 未授权
              }
            },
            fail: error => {
              this.dialogTip('网络连接失败');
            },
          })
        },
        fail: error => {
          this.dialogTip('网络连接失败');
        },
      });
    })
  },

  getUserInfo(loginRes, opt){
    wx.getUserInfo({
        lang: 'zh_CN',
        success: res => {
          // 可以将 res 发送给后台解码出 unionId
          this.globalData.userInfo = res.userInfo
          console.log(res);
          // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
          // 所以此处加入 callback 以防止这种情况
          if (this.userInfoReadyCallback) {
            this.userInfoReadyCallback(res)
          }
          sendUserInfo({
            code: wx.getStorageSync('login_code'),  // 登陆凭证code wx.setStorageSync('login_code'
            extAppId: 'wx0ba806b9709f9b26',
            userinfo: res.userInfo 
          }).then(res => {
            console.log(res);
          });
        },
        fail: error =>{
          console.log(error);
        }
    })
  },

  showSetting(fn){
    wx.showModal({
        title: '提示',
        content: '授权才能正常使用功能，请授权。',
        showCancel: false,
        success: (res) => {
            if (res.confirm) {
                wx.openSetting({
                    success: (res) => {
                        fn && fn();
                    }
                });
            }
        },
        fail:error=>{

        }
    })
  },


  dialogTip(title, time){
    if(!time) time = 1500;
    wx.showToast({
      title: title,
      // image: '/pages/assets/images/false.png',
      duration: time
    })
  },

  handleAudioSrc:function(id){
    var that = this;
    var src = '';
    that.globalData.globalSrcList.find(item => {
      if (item.audioId == id){
        src = item.audioSrc;
      }
    })
    return src;
  },
  globalBackgroundAudioManager: wx.getBackgroundAudioManager(),

  globalData: {
    userInfo: null,
    globalCurrentTime: 0,
    globalProgress: 0,
    globalSrcList: [
      { audioId: 1, audioSrc:'http://122.228.254.5/mp3.9ku.com/mp3/371/370924.mp3'},
      { audioId: 2, audioSrc: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'}],
  },

  


})